package com.foxmail.aroundme.boring.base.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.foxmail.aroundme.boring.annotation.ActivityFragmentInject;
import com.foxmail.aroundme.boring.app.App;
import com.foxmail.aroundme.boring.base.present.BasePre;
import com.foxmail.aroundme.boring.base.view.BaseView;
import com.squareup.leakcanary.RefWatcher;

/**
 * Created by gzl on 12/20/16.
 *
 *
 */

public abstract class BaseFragment<T extends BasePre> extends Fragment implements BaseView{

    protected T mPresenter;

    //界面View
    protected View view;

    //布局ID
    protected int mContentViewId;

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == view) {
            if (getClass().isAnnotationPresent(ActivityFragmentInject.class)) {
                ActivityFragmentInject annotation = getClass().getAnnotation(ActivityFragmentInject.class);
                mContentViewId = annotation.contentViewId();
            } else {
                throw new RuntimeException("Class must add annotations of ActivityFragmentInitParams.class");
            }
            view = inflater.inflate(mContentViewId, container, false);
        }
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onResume();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }

    /**
     * 初始化控件
     *
     * @param fragmentRootView fragment根目录
     */
    protected abstract void initView(View fragmentRootView);


}
